package com.rpj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.rpj.entity.StuDto;
import com.rpj.entity.StuEntity;
import com.rpj.entity.TeaDto;
import com.rpj.entity.TeaEntity;
import com.rpj.service.TeaService;

@RestController
public class TeaController {
@Autowired
public TeaService teaservice;
@RequestMapping("/Teaquery")
public Object query(TeaDto dto,int page, int limit) {
	
	Pageable pageable = PageRequest.of(page-1, limit);
	Page<TeaEntity> pageTea = teaservice.queryByCondition(dto, pageable);
	HashMap<String,Object> map = new HashMap<String,Object>();

	map.put("code", 0);
	map.put("msg", "");
	map.put("count", pageTea.getTotalElements());
	map.put("data", pageTea.getContent());
	
	
	return map;
}




	
	

@RequestMapping("/Teaupdate")
public Object update(String teaname,String teapassword,String teadept,String status,String teaborrowbooknum,String teaborrowmaxnum,String teagivebooknum,String data1,String data2,String data3,String teaid)
 {
	int i = teaservice.updateTea(teaname, teapassword, teadept, status, teaborrowbooknum, teaborrowmaxnum, teagivebooknum, data1, data2, data3, teaid);
	if(i>0) {
		return true;
	}else {
		return false;
	}
}
@RequestMapping("/Teasave")
public Object save(TeaEntity e) {
	TeaEntity save = teaservice.save(e);
	if(save!=null) {
		return true;
	}else {
		return false;
	}
}
//部门
@RequestMapping("/querydept")
public Object querydept() {
	List<String> querydept = teaservice.querydept();
	return querydept;
}
//封装（图表）
@RequestMapping(value="/teaqueryBar",method =RequestMethod.POST)
public Object queryBar() {
	List<TeaEntity> list = teaservice.query();
	List<String> teaname=new ArrayList<>();
	List<String> teaborrowbooknum=new ArrayList<>();
	List<String> teagivebooknum=new ArrayList<>();

	for (TeaEntity tea : list) {
		teaname.add(tea.getTeaname());
		teaborrowbooknum.add(tea.getTeaborrowbooknum());
		teagivebooknum.add(tea.getTeagivebooknum());
	}
	Map<String,Object> map = new HashMap<String, Object>();
	map.put("name",teaname );
	map.put("borrow", teaborrowbooknum);
	map.put("given", teagivebooknum);
	return map;
}
/**
 * 登陆查询
 */
@RequestMapping(value="/tealogin",method =RequestMethod.GET)
public Object tealogin(String teaname,String teapassword) {
	TeaEntity i = teaservice.tealogin(teaname, teapassword);
	HashMap<String, Object> map = new HashMap<String, Object>();
	ArrayList<TeaEntity> list = new ArrayList<TeaEntity>();
	list.add(i);
	map.put("code",0);
	map.put("data", list);
	map.put("count",1);
	map.put("msg","");
	if(i!=null) {
		return map;
	}else {
		return map;
	}
}


}
